/*
 * DropHandler.java
 *
 * Created on August 30, 2007, 8:22 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.afaker.rss.gui.dnd;

import com.afaker.rss.*;
import com.afaker.rss.gui.tree.RSSTreeNode;
import java.awt.Point;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.IOException;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author bruce
 */
public class DropHandler implements DropTargetListener{
    private JTree tree;
    private DropTarget target;
    
    /** Creates a new instance of DropHandler */
    public DropHandler(JTree tree) {
        this.tree = tree;
        target = new DropTarget(tree,this);
    }
    
    public void dragEnter(DropTargetDragEvent dtde) {
        dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
    }
    
    public void dragOver(DropTargetDragEvent dtde) {
        
    }
    
    public void dropActionChanged(DropTargetDragEvent dtde) {
    }
    
    public void dragExit(DropTargetEvent dte) {
    }
    
    public void drop(DropTargetDropEvent dtde) {
        Point p = dtde.getLocation();
        TreePath path = tree.getPathForLocation(p.x,p.y);
        dtde.acceptDrop(DnDConstants.ACTION_MOVE);
        boolean dropped = false;
        try {
            Transferable t = dtde.getTransferable();
            Object droppedObject = t.getTransferData(TransferableObject.localObjectFlavor);
            RSSTreeNode droppedNode = null;
            if(droppedObject instanceof RSSTreeNode){
                droppedNode = (RSSTreeNode) droppedObject;
                ((DefaultTreeModel)tree.getModel()).removeNodeFromParent(droppedNode);
            } else{
                droppedNode = new RSSTreeNode();
            }
            if(path == null)
                return ;
            DefaultMutableTreeNode dropNode = (DefaultMutableTreeNode) path.getLastPathComponent();
            if(dropNode instanceof RSSTreeNode) {
                
                if(((RSSTreeNode)dropNode).isFeed()) {
                    DefaultMutableTreeNode parent = (DefaultMutableTreeNode) dropNode.getParent();
                    int index = parent.getIndex(dropNode);
                    ((DefaultTreeModel)tree.getModel()).insertNodeInto(droppedNode,parent,index);
                } else {
                    ((DefaultTreeModel)tree.getModel()).insertNodeInto(droppedNode,dropNode,dropNode.getChildCount());                  
                }
                dropped = true;
            }
            
        } catch (UnsupportedFlavorException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        dtde.dropComplete(dropped);        
    }    
}
